
package com.insigma.business.qrcode.welfare._provider.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import com.insigma.business.qrcode.common._provider.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.util.SpringContextUtil;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.welfare.dto.*;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.welfare.dto.request.MarketingSpotSearchRequest;
import com.insigma.business.qrcode.welfare.entity.MarketingSpotDO;
import com.insigma.business.qrcode.welfare.entity.MarketingSpotMaterialsDO;
import com.insigma.business.qrcode.welfare.service.MarketingSpotMaterialsService;
import com.insigma.business.qrcode.welfare.service.MarketingSpotMaterialsTransform;
import com.insigma.business.qrcode.welfare._provider.MarketingSpotProvider;
import com.insigma.business.qrcode.welfare.service.MarketingSpotService;
import com.insigma.business.qrcode.common.converter.MarketingSpotProviderConverter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;


@Slf4j
@Service
@Component("marketingSpotProvider")
public class MarketingSpotProviderImpl extends BaseProviderImpl implements MarketingSpotProvider {

    @Autowired
    private MarketingSpotService marketingSpotService;

    @Autowired
    private MarketingSpotMaterialsService marketingSpotMaterialsService;

    @Override
    public BasePagingResponse<List<MarketingSpotDTO>> searchMarketingSpot(final MarketingSpotSearchRequest searchRequest) {

        final BasePagingResponse<List<MarketingSpotDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页MarketingSpot的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final MarketingSpotBSearcher marketingSpotBSearcher = MarketingSpotProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<MarketingSpotDO> marketingSpotDOList = marketingSpotService.search(marketingSpotBSearcher);
                final List<MarketingSpotDTO> marketingSpotDTOList = MarketingSpotProviderConverter.convertResponseDOList2DTOList(marketingSpotDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, marketingSpotDTOList, searchRequest, marketingSpotBSearcher.getTotalCount());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.MARKETING_SPOT_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<MarketingSpotDTO> getMarketingSpot(final BaseGetRequest baseGetRequest) {

        final BaseResponse<MarketingSpotDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条MarketingSpot的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final MarketingSpotDO marketingSpotDO = marketingSpotService.get((String) baseGetRequest.getId());
                final MarketingSpotDTO remoteObj = MarketingSpotProviderConverter.convertResponseDO2DTO(marketingSpotDO);
                ResponseUtils.setupSuccessResponse(baseResponse, remoteObj);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.MARKETING_SPOT_GET;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<MarketingSpotDeliveryDTO> deliveryMaterial(MarketingSpotSearchRequest request) {

        BizCheckUtil.checkNotNull(request, "请求不能为空");
        BizCheckUtil.checkNotNull(request.getCode(), "code不能为空");

        MarketingSpotDeliveryDTO remoteObj = new MarketingSpotDeliveryDTO();

        MarketingSpotDO marketingSpotDO = marketingSpotService.getByCode(request.getCode());
        if (marketingSpotDO == null) {
            return ResponseUtils.buildSuccessResponse(remoteObj);
        }
        BeanUtils.copyProperties(marketingSpotDO, remoteObj);

        final MarketingSpotBSearcher marketingSpotBSearcher = MarketingSpotProviderConverter.convertRequestSO2Searcher(request);
        List<MarketingSpotMaterialsDO> marketingSpotMaterialsDOS = marketingSpotMaterialsService.listBySearcher(marketingSpotBSearcher);

        if (CollectionUtils.isNotEmpty(marketingSpotMaterialsDOS)) {
            List<MarketingSpotMeterialDetailBO> list = Lists.newArrayList();
            for (MarketingSpotMaterialsDO marketingSpotMaterialsDO : marketingSpotMaterialsDOS) {
                String materials = marketingSpotMaterialsDO.getMaterials();

                if (Boolean.TRUE.equals(marketingSpotMaterialsDO.getTransform())) {

                    MarketingSpotMaterialsTransform bean = (MarketingSpotMaterialsTransform) SpringContextUtil.getBean(marketingSpotMaterialsDO.getBean());
                    MarketingSpotCommonInput input = new MarketingSpotCommonInput();
                    input.setSoldierId(request.getSoldierId());
                    input.setMaterials(materials);
                    materials = bean.transform(input);

                    if (Strings.isBlank(materials)) {
                        continue;
                    }
                }

                MarketingSpotMeterialDetailBO marketingSpotMeterialDetailBO = JSONObject.parseObject(materials, MarketingSpotMeterialDetailBO.class);
                marketingSpotMeterialDetailBO.setSeqNo(marketingSpotMaterialsDO.getSeqNo());
                list.add(marketingSpotMeterialDetailBO);
            }
            Collections.sort(list, new Comparator<MarketingSpotMeterialDetailBO>() {
                @Override
                public int compare(MarketingSpotMeterialDetailBO o1, MarketingSpotMeterialDetailBO o2) {
                    return o1.getSeqNo().compareTo(o2.getSeqNo());
                }
            });
            remoteObj.setMeterials(list);
        }

        return ResponseUtils.buildSuccessResponse(remoteObj);
    }
}
